package com.vlong.town.controller.admin.hotel;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.hotel.HotelInfo;
import com.vlong.town.service.hotel.HotelInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * 酒店民宿信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/hotel")
public class HotelInfoController extends BaseController {

    // Fields

    @Autowired
    private HotelInfoService hotelInfoService;// 酒店民宿信息业务实现类

    /**
     * 进入酒店民宿信息列表页面
     * @return hotel/list_hotel
     */
    @RequestMapping("/list")
    public String gotoHotelInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc,
            @RequestParam(required=false, value="type") String type
    		) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        Integer  hotelType=null;
	    if(type!=null&&!type.equals("-1")){
	    	hotelType = Integer.parseInt(type);
	    }
        List<HotelInfo> hotelInfoList = this.hotelInfoService.queryList(title, hotelType,content, desc,2);
        PageInfo<HotelInfo> pageInfo = new PageInfo<HotelInfo>(hotelInfoList);
        PageTag<HotelInfo> pt = new PageTag<HotelInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        request.setAttribute("type", type);
        request.setAttribute("status", 2);
        for(HotelInfo hotelInfo : hotelInfoList) {
            hotelInfo.setContent(DataUtil.delHTMLTag(hotelInfo.getContent(), 0));
        }
        request.setAttribute("hotelInfoList", hotelInfoList);
        return "admin/hotel/list_hotel";
    }
    
    /**
     * 进入酒店民宿信息列表页面
     * @return hotel/list_hotel
     */
    @RequestMapping("/listNo")
    public String listNo(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content,
            @RequestParam(required=false, value="desc") String desc,
            @RequestParam(required=false, value="type") String type
    		) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        Integer  hotelType=null;
	    if(type!=null&&!type.equals("-1")){
	    	hotelType = Integer.parseInt(type);
	    }
	    List<HotelInfo> hotelInfoList =new  ArrayList<HotelInfo>();
        List<HotelInfo> hotelInfoListOne = this.hotelInfoService.queryList(title, hotelType,content, desc,1);
        List<HotelInfo> hotelInfoListTwo = this.hotelInfoService.queryList(title, hotelType,content, desc,3);
        hotelInfoList.addAll(hotelInfoListOne);
        hotelInfoList.addAll(hotelInfoListTwo);
        PageInfo<HotelInfo> pageInfo = new PageInfo<HotelInfo>(hotelInfoList);
        PageTag<HotelInfo> pt = new PageTag<HotelInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        request.setAttribute("desc", desc);
        request.setAttribute("type", type);
        request.setAttribute("status", 1);
        for(HotelInfo hotelInfo : hotelInfoList) {
            hotelInfo.setContent(DataUtil.delHTMLTag(hotelInfo.getContent(), 0));
        }
        request.setAttribute("hotelInfoList", hotelInfoList);
        return "admin/hotel/list_hotel";
    }

    /**
     * 进入酒店民宿信息添加页面
     * @return hotel/add_hotel
     */
    @GetMapping("/addHotel")
    public String gotoHotelInofAddPage() {
        return "admin/hotel/add_hotel";
    }

    /**
     * 进入酒店民宿信息修改页面
     * @return hotel/edit_hotel
     */
    @GetMapping("/editHotel/{hotelId}")
    public String gotoHotelInofEditPage(@PathVariable(required=true, value="hotelId") Long hotelId) {
        HotelInfo hotelInfo = this.hotelInfoService.queryById(hotelId);
        request.setAttribute("hotelInfo", hotelInfo);
        request.setAttribute("thumbnail",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + hotelInfo.getThumbnail());
        return "admin/hotel/edit_hotel";
    }

    /**
     * 添加酒店民宿信息
     * @param title 酒店民宿标题
     * @param content 酒店民宿内容
     * @param desc 酒店民宿简介
     * @param picture 缩略图
     * @param feature 酒店民宿特征
     * @param address 酒店民宿位置
//     * @param lng 酒店民宿经度
//     * @param lat 酒店民宿纬度
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddHotel")
    public DataMessage addHotel(
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="feature", required=true) String feature,
            @RequestParam(name="type", required=true) String type,
            @RequestParam(name="address", required=true) String address)
//            @RequestParam(name="lng", required=true) String lng,
//            @RequestParam(name="lat", required=true) String lat
    {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("酒店民宿标题不合法");
        }else if(type.equals("-1")){
        	dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("酒店类型不合法");
        }else{
        	Integer  hotelType=null;
   	     	if(type!=null&&!type.equals("-1")){
   	     	hotelType = Integer.parseInt(type);
   	     	}
        	this.hotelInfoService.add(1L,hotelType, title, content, desc, picture, feature, address, null, null);
        	dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        	dm.setMessage("酒店民宿添加成功");
        }
        return dm;
    }

    /**
     * 修改酒店民宿信息
     * @param title 酒店民宿标题
     * @param content 酒店民宿内容
     * @param desc 酒店民宿简介
     * @param picture 缩略图
     * @param feature 酒店民宿特征
     * @param address 酒店民宿位置
//     * @param lng 酒店民宿经度
//     * @param lat 酒店民宿纬度
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditHotel")
    public DataMessage editHotel(
            @RequestParam(name="hotelId", required=true) Long hotelId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="desc", required=true) String desc,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="feature", required=true) String feature,
            @RequestParam(name="type", required=true) String type,
            @RequestParam(name="address", required=true) String address)
//            @RequestParam(name="lng", required=true) String lng,
//            @RequestParam(name="lat", required=true) String lat
    {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("酒店民宿标题不合法");
        }else if(type.equals("-1")){
        	dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("酒店类型不合法");
        }else{
        	
        	Integer  hotelType=null;
   	     	if(type!=null&&!type.equals("-1")){
   	     	hotelType = Integer.parseInt(type);
   	     	}
        	this.hotelInfoService.update(hotelId, hotelType,title, content, desc, picture, feature, address, null, null,null,null);
        	dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        	dm.setMessage("酒店民宿修改成功");
        }
        return dm;
    }

    /**
     * 删除酒店民宿
     * @param hotelId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="hotelId") Long hotelId) {
        DataMessage dm = new DataMessage();
        if(hotelId != null && hotelId > 0L) {
            this.hotelInfoService.delete(hotelId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("酒店民宿删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除酒店民宿
     * @param hotelIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="hotelIds") String hotelIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(hotelIds, 1, 255)) {
            String[] hotelIdArray = hotelIds.split(",");
            for(String hotelId : hotelIdArray) {
                this.hotelInfoService.delete(Long.parseLong(hotelId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("酒店民宿删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
    
    /**
     * 通过酒店民宿
     * @param hotelId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/adoptRestaurant")
    public DataMessage adoptRestaurant(@RequestParam(required=true, value="hotelId") Long hotelId) {
        DataMessage dm = new DataMessage();
        if(hotelId != null && hotelId > 0L) {
            this.hotelInfoService.update(hotelId, null, null, null, null, null, null, null, null, null,2,null);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("酒店民宿通过成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
    
    /**
	 * 驳回
	 * @return
	 */
	@RequestMapping(value="/rejectHotel")
	public  String  rejectHotel(@RequestParam(required=true, value="hotelId") Long hotelId){
		request.setAttribute("hotelId", hotelId);
		return "admin/hotel/reject_hotel";
	}
	
	 /**
     * 更新驳回原因
     * @param hotelId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/updateReason")
    public DataMessage updateReason(
    		@RequestParam(required=true, value="hotelId") Long hotelId,
    		@RequestParam(required=true, value="reason") String reason
    		) {
        DataMessage dm = new DataMessage();
        if(hotelId != null && hotelId > 0L) {
            this.hotelInfoService.update(hotelId, null, null, null, null, null, null, null, null, null,3,reason);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("酒店民宿驳回成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }
    
    /**
	 * 查看驳回原因
	 * @return
	 */
	@RequestMapping(value="/viewHotel")
	public  String  viewHotel(@RequestParam(required=true, value="hotelId") Long hotelId){
		HotelInfo hotel = this.hotelInfoService.queryById(hotelId);
		request.setAttribute("hotel", hotel);
		return "admin/hotel/view_hotel";
	}
    
    
    
    
}
